/*
GNU Lesser General Public License

AlignAction
Copyright (C) 2009 Howard Kistler

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package com.hexidec.ekit.action;

import java.awt.event.ActionEvent;
import javax.swing.text.StyledEditorKit;

import com.hexidec.ekit.EkitCore;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument.BlockElement;



public class AlignAction extends StyledEditorKit.AlignmentAction
{
	protected EkitCore parent;
        int align = StyleConstants.ALIGN_RIGHT;

	public AlignAction(EkitCore parent, String actionName, int align)
	{
		super(actionName, align);
		this.parent = parent;
                this.align = align;
	}

	public void actionPerformed(ActionEvent ae)
	{
//		int selStart = parent.getTextPane().getSelectionStart();
//		int selEnd = parent.getTextPane().getSelectionEnd();
//		super.actionPerformed(ae);
//		parent.getTextPane().setText(parent.getTextPane().getText());
//		parent.get  TextPane().setSelectionStart(selStart);
//		parent.getTextPane().setSelectionEnd(selEnd);


            try {
                BlockElement e= (BlockElement)parent.getTextPane().getDocument().getDefaultRootElement();
                BlockElement body=null;
                body = (BlockElement)findElement(e,"body");
                if(body!=null){
                    SimpleAttributeSet sas  = new SimpleAttributeSet();
                    StyleConstants.setAlignment(sas, align);
                    parent.getExtendedHtmlDoc().replaceAttributes(body,sas, HTML.Tag.BODY);
                    parent.getTextPane().updateUI();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            //SimpleAttributeSet atts = new SimpleAttributeSet();
            //StyleConstants.setAlignment(atts, align);
            //setParagraphAttributes(parent.getTextPane(), atts, false);

	}

    public BlockElement findElement(BlockElement e, String tagName) {
        if (e.getName().equalsIgnoreCase(tagName)) {
            return e;
        }
        BlockElement found = null;
        for (int i = 0; i <= e.getChildCount(); i++) {
            if(e.getChildAt(i) instanceof BlockElement){
                found =  findElement((BlockElement)e.getChildAt(i), tagName);
                if(found!=null) break;
            }
        }

        return found;
    }




}